﻿using System;
using System.Windows.Forms;
using KryptecizerBase;
using KryptecizerBase.Encryption;
using KryptecizerBase.Decryption;
using Kryptonalysis;
using KryptecizerBase.EventArguments;
using Kryptonalysis.EventArguments;
using System.Collections;
using System.Collections.Generic;
using Kryptonalysis.Statistics;
using System.Threading;

namespace testapp
{
    public partial class Form1 : Form
    {
        TextBasedKryptObject kryptObj = new TextBasedKryptObject();
        KryptoTextEncryptor encr;
        KryptoTextDecryptor decr;

        public Form1()
        {
            InitializeComponent();
        }

        void decr_DecryptionEndet(bool successful)
        {
            kryptObj.kryptoTextDecrypted.Fill(decr.DecryptedKryptoText.GetAsString());
        }

        void kryptObj_EncryptedTextChanged(TextChangedEventArgs arg)
        {
            memoEdit2.Text = arg.Text;
        }

        void kryptObj_DecryptedTextChanged(TextChangedEventArgs arg)
        {
            memoEdit1.Text = arg.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            kryptObj.kryptoTextDecrypted.Fill(memoEdit1.Text);
            encr = new VigenereEncryptor(textBox1.Text);
            encr.EncryptionEnded += new Encryptor.EncryptionEndedEventHandler(encr_EncryptionEndet);
            kryptObj.EncryptedTextChanged += new TextBasedKryptObject.EncryptedTextChangedHandler(kryptObj_EncryptedTextChanged);
            kryptObj.Encrypt(encr);
        }

        void encr_EncryptionEndet(bool successful)
        {
            kryptObj.kryptoTextEncrypted.Fill(encr.EncryptedKryptoText.GetAsString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            kryptObj.kryptoTextEncrypted.Fill(memoEdit2.Text);
            decr = new VigenereDecryptor(textBox1.Text);
            decr.DecryptionEnded += new Decryptor.DecryptionEndedEventHandler(decr_DecryptionEndet);
            kryptObj.DecryptedTextChanged += new TextBasedKryptObject.DecryptedTextChangedHandler(kryptObj_DecryptedTextChanged);
            kryptObj.Decrypt(decr);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            KryptoText kt = new KryptoText();
            kt.Fill(memoEdit2.Text);
            CaesarAnalyzer analyzer = new CaesarAnalyzer();
            analyzer.AnalysisEndet += new Analyzer.AnalysisEndetEventHandler(analyzer_AnalysisEndet);
            analyzer.EncryptedKryptoText = kt;
            analyzer.DetectPassword();
            
        }
        
        void analyzer_AnalysisEndet(AnalysisEndedEventArgs args)
        {
            if (textBox1.InvokeRequired)
                textBox1.Invoke(new MethodInvoker(delegate { SetPwText(args.PW); }));
            else
                SetPwText(args.PW);

            if (textBox1.InvokeRequired)
                textBox1.Invoke(new MethodInvoker(delegate { SetAnalysisStatistc(args.Stats); }));
            else
                SetAnalysisStatistc(args.Stats);
        }

        private void SetPwText(string text)
        {
            textBox1.Text = text;
        }

        private void SetAnalysisStatistc(AnalysisStatistic stats)
        {
            if (stats != null)
            {
                chartControl1.Series.Clear();

                if (stats.Dividors != null)
                {

                    DevExpress.XtraCharts.Series s = new DevExpress.XtraCharts.Series("", DevExpress.XtraCharts.ViewType.Bar);
                    s.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.Numerical;
                    s.SeriesPointsSorting = DevExpress.XtraCharts.SortingMode.Ascending;
                    s.ValueScaleType = DevExpress.XtraCharts.ScaleType.Numerical;

                    foreach (KeyValuePair<int, int> valItem in stats.Dividors)
                        if (valItem.Key < 50)
                            s.Points.Add(new DevExpress.XtraCharts.SeriesPoint(valItem.Key, valItem.Value));

                    s.Label.Visible = false;
                    chartControl1.Series.Add(s);
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(teststart));
            t.Start();
        }

        private void teststart()
        {
            KryptoText t = new KryptoText();
            t.Fill(memoEdit2.Text);
            VigenereAnalyzer lyze = new VigenereAnalyzer();
            lyze.AnalysisProcessing += new Analyzer.AnalysisProcessingEventHandler(lyze_AnalysisProcessing);
            lyze.AnalysisEndet += new Analyzer.AnalysisEndetEventHandler(analyzer_AnalysisEndet);
            lyze.EncryptedKryptoText = t;
            lyze.DetectPassword();
        }

        void lyze_AnalysisProcessing(int percentDone)
        {
            if (progressBarControl1.InvokeRequired)
                progressBarControl1.Invoke(new MethodInvoker(delegate { SetPercents(percentDone); }));
            else
                SetPercents(percentDone);
        }

        private void SetPercents(int perc)
        {
            progressBarControl1.Position = perc;
        }
    }
}
